package com.rjt.user.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.rjt.permission.Permission;
import com.rjt.user.model.UserModel;
import com.rjt.user.service.UserService;

@Controller
@RequestMapping("user")
public class UserController {
	
	@Autowired
	private UserService userService;
	
	@GetMapping("userList")
	@ResponseBody
	@Permission
	public Page<UserModel>  userList(HttpServletRequest request){
		return userService.getList(request);
	}
	
	@GetMapping("list")
	@Permission
	public String list(HttpSession session) {
		return "user/list";
	}
	
	
	@ResponseBody
	@PostMapping("delUser")
	@Permission
	public String delUserModel(@RequestBody List<UserModel> user) {
		return userService.delete(user);
	}
	
	@GetMapping("toUpdatePage")
	@Permission
	public String toUpdatePage(int id,HttpServletRequest request) {
		UserModel user = userService.findUser(id);
		request.setAttribute("user",user);
		return "user/edit";
	}
	
	@PostMapping("editUser")
	@ResponseBody
	@Permission
	public String editUserModel(@RequestBody UserModel user) {
		return userService.add(user,"edit");
	}
	
	@GetMapping("toNewPage")
	@Permission
	public String toNewPage(HttpSession session) {
		return "user/add";
	}
	
	@PostMapping("addUser")
	@ResponseBody
	@Permission
	public String addArticle(@RequestBody UserModel user) {
		return userService.add(user,"save");
	}
	
	@GetMapping("toIndex")
	public String toIndex() {
		if(!userService.isLogin()) {
			return "user/login";
		}
		return "admin/index";
	}
}
